package com.hundsun.hsOffer.controller;

import com.hundsun.hsOffer.model.SysDict;
import com.hundsun.hsOffer.model.SysLog;
import com.hundsun.hsOffer.utils.Combobox;
import com.hundsun.hsOffer.utils.DataGrid;
import com.hundsun.hsOffer.utils.DateUtils;
import com.jfinal.core.Controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Create By IDEA
 * User:giegie
 * Date:2016/8/18
 * Description:系统模块控制器
 */
public class SystemController extends Controller {
    /**
     * 日志展示
     */
    public void sysLogListUI() {
        render("sysLogList.ftl");
    }

    public void sysLogList() {
        DataGrid<SysLog> dataGrid = SysLog.dao.findByPage(getParaToInt("page"), getParaToInt("rows"));
        renderJson(dataGrid);
    }

    /**
     * 字典展示
     */
    public void sysDictListUI() {
        render("sysDictList.ftl");
    }

    public void sysDictList() {
        DataGrid<SysDict> dataGrid = SysDict.dao.findByPage(getParaToInt("page"), getParaToInt("rows"));
        renderJson(dataGrid);
    }

    public void addDictUI() {
        render("dictForm.ftl");
    }

    public void addDict() {
        SysDict dict = getModel(SysDict.class, "", true);
        dict.setCreateTime(DateUtils.formatDate(new Date()));
        renderJson(dict.save());
    }

    public void editDictUI(){
        Integer id = getParaToInt(0);
        SysDict dict = SysDict.dao.findById(id);
        setAttr("dict",dict);
        render("dictEditForm.ftl");
    }

    public void editDict(){
        SysDict dict = getModel(SysDict.class,"",true);
        renderJson(dict.update());
    }

    public void delDict(){
        Integer id = getParaToInt(0);
        SysDict dict = SysDict.dao.findById(id);
        dict.setDelFlag(1);
        dict.update();
        renderJson(true);
    }

    /**
     * 查询字典 返回json串
     */
    public void findDicts() {
        String type = getPara(0);
        List<SysDict> dictList = SysDict.dao.findByType(type);
        List<Combobox> comboboxList = new ArrayList<Combobox>();
        for (SysDict dict : dictList) {
            Combobox combobox = new Combobox();
            combobox.setId(dict.getDictId().toString());
            combobox.setTextField(dict.getLabelName());
            combobox.setValueField(dict.getLabelValue());
            comboboxList.add(combobox);
        }
        renderJson(comboboxList);
    }



}
